package com.eagle.coders.common.ui.form.renderer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.dom4j.Element;

import com.eagle.coders.common.ui.form.renderer.cache.UIDomainPropertyBean;

/**
 * 
 * @author Anees
 * 
 */
public class TableWidget {

	private SortedMap<Integer, String> uiDomainMap;

	private Map<Integer, String> colTypeMap;

	public TableWidget() {

	}

	/**
	 * 
	 * @param uiDomainPropertyHolderList
	 * @param tableNode
	 */
	public void createTable(
			List<UIDomainPropertyBean> uiDomainPropertyHolderList,	Element tableNode) {

		uiDomainMap = new TreeMap<Integer, String>();

		colTypeMap = new HashMap<Integer, String>();
		
		initTable(uiDomainPropertyHolderList);

		createTableColumnNode(tableNode);
	}
	
	/**
	 * 
	 * @param uiDomainPropertyHolderList
	 */
	private void initTable(List<UIDomainPropertyBean> uiDomainPropertyHolderList){
		
		for (UIDomainPropertyBean uiDomainHolder : uiDomainPropertyHolderList) {

			if (null != uiDomainHolder.getTableDisplayPosition()) {
				uiDomainMap.put(uiDomainHolder.getTableDisplayPosition(), uiDomainHolder.getUiPropertyLabel());
				colTypeMap.put(uiDomainHolder.getTableDisplayPosition(), uiDomainHolder.getUiPropertyType());
			}
		}
	}
	
	/**
	 * 
	 * @param tableNode
	 */
	private void createTableColumnNode(Element tableNode) {

		Set<Integer> keys = uiDomainMap.keySet();

		for (Integer key : keys) {

			Element tableColumn = tableNode.addElement("table-column");

			tableColumn.addAttribute("position", key.toString());

			tableColumn.addAttribute("label", uiDomainMap.get(key));

			tableColumn.addAttribute("colType", colTypeMap.get(key));

			// TODO: for data-type node i will do it latter
		}

	}
}